#!/usr/bin/env python
from __future__ import (absolute_import, division, print_function)
from PIL import Image
import six

import imagehash
# import jsonify

"""
Demo of hashing
"""


def find_similar_images(userpaths, hashfunc=imagehash.average_hash):
    import os

    def is_image(filename):
        f = filename.lower()
        return f.endswith(".png") or f.endswith(".jpg") or \
            f.endswith(".jpeg") or f.endswith(
                ".bmp") or f.endswith(".gif") or '.jpg' in f

    image_filenames = []
    for userpath in userpaths:
        image_filenames += [os.path.join(userpath, path)
                            for path in os.listdir(userpath) if is_image(path)]
    images = {}
    for img in sorted(image_filenames):
        try:
            hash = hashfunc(Image.open(img))
        except Exception as e:
            print('Problem:', e, 'with', img)
        if hash in images:
            print(img, '  already exists as', ' '.join(images[hash]))

        images[hash] = images.get(hash, []) + [img]

    return images


def save_doubles(images):
    from shutil import copyfile
    for hash in images:
        if(len(images[hash]) >= 2):
            save_dir = './tmp/'+str(hash)
            try:
                os.mkdir(save_dir)
            except FileExistsError:
                pass
            for image in images[hash]:
                # save_dir = os.path.dirname(image)+'/'+str(hash)
                # try:
                #     os.mkdir(save_dir)
                # except FileExistsError:
                #     pass
                save_path = save_dir+'/'+os.path.basename(image)
                print('Copying', image, ' in', save_path)
                copyfile(image, save_path)


if __name__ == '__main__':
    import sys
    import os

    def usage():
        sys.stderr.write("""SYNOPSIS: %s [ahash|phash|dhash|...] [<directory>]

Identifies similar images in the directory.

Method: 
  ahash:      Average hash
  phash:      Perceptual hash
  dhash:      Difference hash
  whash-haar: Haar wavelet hash
  whash-db4:  Daubechies wavelet hash

(C) Johannes Buchner, 2013-2017
""" % sys.argv[0])
        sys.exit(1)

    hashmethod = sys.argv[1] if len(sys.argv) > 1 else usage()
    if hashmethod == 'ahash':
        hashfunc = imagehash.average_hash
    elif hashmethod == 'phash':
        hashfunc = imagehash.phash
    elif hashmethod == 'dhash':
        hashfunc = imagehash.dhash
    elif hashmethod == 'whash-haar':
        hashfunc = imagehash.whash
    elif hashmethod == 'whash-db4':
        def hashfunc(img): return imagehash.whash(img, mode='db4')
    else:
        usage()
    userpaths = sys.argv[2:] if len(sys.argv) > 2 else "."
    images = find_similar_images(userpaths=userpaths, hashfunc=hashfunc)
    # json = jsonify(images)
    # Save json? Or pickle
    save_doubles(images)
